#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#define MAXBUF 256 
//#define PORT 5000 
//#define GROUP "224.0.1.21" 
int main(void) { 
	char tmp,GROUP[50]="224.0.1.21";
	int PORT=5000;
	printf("Set configuration now?(if you choose not, default configuration will be used.) y/n");
	scanf("%c",&tmp);
	if(tmp=='y')
	{
		printf("Input group address:");
		scanf("%s",GROUP);
		printf("Input port number:");
		scanf("%d",&PORT);
	}
	
	int s, n, r; 
	struct sockaddr_in srv, cli; 
	struct ip_mreq mreq; 
	char buf[MAXBUF]; 
	bzero(&srv, sizeof(srv)); 
	srv.sin_family = AF_INET; 
	srv.sin_port = htons(PORT); 
	if (inet_aton(GROUP, &srv.sin_addr) < 0) { 
		perror("inet_aton"); 
		return 1; 
	} 
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 
		perror("socket"); 
		return 1; 
	} 
	if (bind(s, (struct sockaddr *)&srv, sizeof(srv)) < 0) { 
		perror("bind"); 
		return 1; 
	} 
	if (inet_aton(GROUP, &mreq.imr_multiaddr) < 0) { 
		perror("inet_aton"); 
		return 1; 
	} 
	mreq.imr_interface.s_addr = htonl(INADDR_ANY); 
	if (setsockopt(s,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq))	< 0) { 
		perror("set sockopt"); 
		return 1; 
	} 
	n = sizeof(cli); 
	while (1) { 
		if ((r = recvfrom(s, buf, MAXBUF, 0, (struct sockaddr *)&cli, &n)) < 0) { 
			perror("recvfrom"); 
		} 
		else { 
			buf[r] = 0; 
			printf( "Receive message: %s  from group: %s\n", buf,inet_ntoa(cli.sin_addr)); 
		} 
	} 
} 
